package org.apache.james.mime4j.decoder;

import java.util.Iterator;

class UnboundedFifoByteBuffer
{
  protected byte[] buffer;
  protected int head;
  protected int tail;

  public UnboundedFifoByteBuffer()
  {
    this(32);
  }

  public UnboundedFifoByteBuffer(int paramInt)
  {
    if (paramInt <= 0)
      throw new IllegalArgumentException("The size must be greater than 0");
    this.buffer = new byte[paramInt + 1];
    this.head = 0;
    this.tail = 0;
  }

  static int access$000(UnboundedFifoByteBuffer paramUnboundedFifoByteBuffer, int paramInt)
  {
    return paramUnboundedFifoByteBuffer.increment(paramInt);
  }

  static int access$100(UnboundedFifoByteBuffer paramUnboundedFifoByteBuffer, int paramInt)
  {
    return paramUnboundedFifoByteBuffer.decrement(paramInt);
  }

  private int decrement(int paramInt)
  {
    int i = paramInt - 1;
    if (i < 0)
      i = this.buffer.length - 1;
    return i;
  }

  private int increment(int paramInt)
  {
    int i = paramInt + 1;
    if (i >= this.buffer.length)
      i = 0;
    return i;
  }

  public boolean add(byte paramByte)
  {
    if (1 + size() >= this.buffer.length)
    {
      byte[] arrayOfByte = new byte[1 + 2 * (this.buffer.length - 1)];
      int i = 0;
      int j = this.head;
      while (true)
      {
        do
        {
          if (j == this.tail)
            break label85;
          arrayOfByte[i] = this.buffer[j];
          this.buffer[j] = 0;
          ++i;
        }
        while (++j != this.buffer.length);
        j = 0;
      }
      label85: this.buffer = arrayOfByte;
      this.head = 0;
      this.tail = i;
    }
    this.buffer[this.tail] = paramByte;
    this.tail = (1 + this.tail);
    if (this.tail >= this.buffer.length)
      this.tail = 0;
    return true;
  }

  public byte get()
  {
    if (isEmpty())
      throw new IllegalStateException("The buffer is already empty");
    return this.buffer[this.head];
  }

  public boolean isEmpty()
  {
    int i;
    if (size() == 0)
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public Iterator iterator()
  {
    return new 1(this);
  }

  public byte remove()
  {
    if (isEmpty())
      throw new IllegalStateException("The buffer is already empty");
    int i = this.buffer[this.head];
    this.head = (1 + this.head);
    if (this.head >= this.buffer.length)
      this.head = 0;
    return i;
  }

  public int size()
  {
    int i;
    if (this.tail < this.head)
      i = this.buffer.length - this.head + this.tail;
    while (true)
    {
      return i;
      i = this.tail - this.head;
    }
  }
}